package com.work.keep.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ListView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.work.keep.R;
import com.work.keep.adapter.MyWorkoutBaseAdapter;
import com.work.keep.base.BaseActivity;
import com.work.keep.datas.WorkoutData;
import com.work.keep.volley.GsonRequest;
import com.work.keep.volley.VolleySingleton;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by dllo on 16/1/16.
 */
public class WorkoutActivity extends BaseActivity {
    private Toolbar toolbar;
    private ListView listView;
    private VolleySingleton volleySingleton;
    private static String id;
    private MyWorkoutBaseAdapter myWorkoutBaseAdapter;

    public static void gotoWorkoutActivity(Context context, String id) {
        Intent intent = new Intent(context, WorkoutActivity.class);
        context.startActivity(intent);
        WorkoutActivity.id = id;
    }

    @Override
    protected View getLayoutView() {
        return null;
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_workout;
    }

    @Override
    protected void initView() {
        toolbar = (Toolbar) findViewById(R.id.workoutToolbar);
        listView = (ListView) findViewById(R.id.workoutListView);
    }

    @Override
    protected void initData() {
        String url = "https://api.gotokeep.com/v1.1/workouthashtag/" + id;
        myWorkoutBaseAdapter = new MyWorkoutBaseAdapter(this);

        listView.setAdapter(myWorkoutBaseAdapter);
        volleySingleton = VolleySingleton.getInstance();
        GsonRequest<WorkoutData> workoutDataGsonRequest = new GsonRequest<WorkoutData>(url, WorkoutData.class, new Response.Listener<WorkoutData>() {
            @Override
            public void onResponse(WorkoutData response) {
                myWorkoutBaseAdapter.addWorkoutDataLists(response.getData().getResults().getPlans());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> workoutHeader = new HashMap<>();
                workoutHeader.put("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1NjY1MDBlMTYzZGIwYzlmNTdiMTQwYWQiLCJ1c2VybmFtZSI6Iuaeq-WQmyIsImF2YXRhciI6Imh0dHA6Ly93eC5xbG9nby5jbi9tbW9wZW4vc2t3MmhSR3lXRWcwUzV0NzhkMlRVUnFhYjBzcWJ4RXB4S1BLUFpEOXpwbTNJbkIySzRjRnhLVEJsemdDb3VIcGlia3RkRUluQ0NFcGpIS2FHZ1lwRkp6SVRIVU5NYTNpYmYvMCIsImlhdCI6MTQ1MjkzMDAxOCwiZXhwIjoxNDU1NTIyMDE4LCJpc3MiOiJodHRwOi8vd3d3LmdvdG9rZWVwLmNvbS8ifQ.gMaASt8gQO3EbffVgZ5M4fAiGfbPu1LK-1OK8osTsTE");
                workoutHeader.put("Host", "api.gotokeep.com");
                workoutHeader.put("X-X", "486bba2566a02d3ae31ed4445366a873b89cf910");
                return super.getHeaders();
            }
        };
        volleySingleton._addRequest(workoutDataGsonRequest);
    }
}
